package com.example.demo.service.impl;

import com.example.demo.entity.User;
import com.example.demo.mapper.usermapper;
import com.example.demo.service.UserService;
import com.example.demo.util.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public  class UserServiceImpl implements UserService {
    @Autowired
    usermapper usermapper;
    @Override
    public User login(User user) {
        User byUsername = usermapper.byUsername(user.getUserName());
        log.info(byUsername.toString());
        if (byUsername==null){
            throw new ServiceException("用户名不存在");
        }
        String password=user.getPassword();
        String md5Password =  DigestUtils.md5DigestAsHex(password.getBytes());
        if (!byUsername.getPassword().equals(md5Password)){
            throw new ServiceException("密码错误");
        }
        //判断is_delete字段的值是否为1表示被标记为删除
        if(byUsername.getStatus()==1){
            throw new ServiceException("用户已注销");
        }
        if(byUsername.getRadio()!=user.getRadio()){
            throw new ServiceException("权限不足");
        }
        return byUsername;
    }

    @Override
    public void reg(User user) {
        User user1 = usermapper.byUsername(user.getUserName());
        if(user1!=null){
             throw new ServiceException("用户名已经存在");
        }
        //密码加密处理：md5算法的形式
        String password=user.getPassword();
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        //将加密之后的密码补全到user对象中
        user.setPassword(md5Password);
        //补全剩下的信息
        user.setStatus(0);
        Date date=new Date();
        user.setCreatedTime(date);
        user.setCreatedUser(user.getUserName());
        user.setModifiedTime(date);
        user.setModifiedUser(user.getUserName());
        //执行注册功能
        Integer insert = usermapper.insertUser(user);
        if(insert!=1){
            throw new ServiceException("用户在注册的过程发生了未知的异常");
        }
    }

    @Override
    public User[] pageUser(int page, int size, int permission,String query) {
        User[] page1 = usermapper.page(page, size, permission,query);
        log.info(123+page1.toString());
        if(page1.length==0){
            throw new ServiceException("暂时没有用户");
        }
        return page1;
    }
    public List<User> all(int permission, String query) {
        List<User> page1 = usermapper.all( permission,query);
        log.info(123+page1.toString());
        if(page1.size() ==0){
            throw new ServiceException("暂时没有用户");
        }
        return page1;
    }

    @Override
    public void update(int id, String name,int status,String username,String username1) {
        User user = usermapper.byUsername(username1);
        //log.info(user.getStatus()+""+status);
        //不知什么时候注销，就判断网名是否相同，如果相同则是该权限请求
        if(user.getName().equals(name))
        status=(status==0) ? 1:0;
        Date date = new Date();
        int update = usermapper.update(name, id,status,username,date);
        log.info("修改2"+id+name+","+update);
        if(update<=0){
            throw new ServiceException("没有修改数据");
        }
    }

    @Override
    public User byUsername(String username) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info("byUsername：用户账号"+username);
        User user = usermapper.byUsername(username);
        user.setCt(dateFormat.format(user.getCreatedTime()));
        user.setMt(dateFormat.format(user.getModifiedTime()));
        return user;
    }
}
